package com.xzs.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xzs.demo.entity.UserDao;
import com.xzs.demo.service.UserDaoService;

@Controller
@RequestMapping("/logins")
public class Login {
	@Autowired
	private UserDaoService uDao;
	
	@GetMapping("/log")
	public String index() {
		UserDao user = uDao.selectByPrimaryKey(1);
		System.out.println(user.toString());
		return "login";
	}
	

	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public boolean login(HttpServletRequest httpRequest,UserDao order) {
		
		UserDao selectByNameAndPwd = uDao.selectByNameAndPwd(order.getUsername(),order.getUserpwd());
		if(selectByNameAndPwd==null){
			return false;
		}else{
			selectByNameAndPwd.setUserpwd(null);
			HttpSession session = httpRequest.getSession();
			session.setMaxInactiveInterval(-1);//session永不失效
			session.setAttribute("user", selectByNameAndPwd);
		return true;
		}
	}
}
